<?php
/**
 * 预立项申请
 */
require_once("../global.inc.php");

class SysProjectClass extends SystemFrameWork{
	/**
	 * 预立项申请对象
	 *
	 * @var obj
	 */
	var $obj_project;
	/**
	 * 审批对象
	 *
	 * @var obj
	 */
	var $obj_project_verify;
	/*
	 *公司
	 */
	var $obj_company;
	/*
	 *公司
	 */
	var $obj_customer;
	/*
	 *公司
	 */
	var $obj_system_power;
	var $obj_department;
	var $obj_member;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	var $level = array('一级', '二级', '三级', '四级', '五级', '六级', '七级', '八级', '九级', '十级');
	
	function main(){
		/**
		 * 客户联系人
		 */
		if (!is_object($this->obj_customer)){
			require_once ("customer.class.php");
			$this->obj_customer = new CustomerClass();
		}
		/**
		 * 客户公司
		 */
		if (!is_object($this->obj_company)){
			require_once ("company.class.php");
			$this->obj_company = new CompanyClass();
		}
		/**
		 * 员工
		 */
		if (!is_object($this->obj_system_power)){
			require_once ("system_power.class.php");
			$this->obj_system_power = new SystemPowerClass();
		}
		/**
		 * 项目
		 */
		if (!is_object($this->obj_project)){
			require_once ("project.class.php");
			$this->obj_project = new ProjectClass();
		}
		/**
		 * 项目审批
		 */
		if (!is_object($this->obj_project_verify)){
			require_once ("project_verify.class.php");
			$this->obj_project_verify = new ProjectVerifyClass();
		}
		if (!is_object($this->ojb_department))
		{
			require_once('department.class.php');
			$this->obj_department = new DepartmentClass();
		}
		if(!is_object($this->obj_member)){
			require_once("member.class.php");
			$this->obj_member = new MemberClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "mylist":
				$this->_mylist();
				break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case 'view':
				$this->_view();
				break;
			case "del":
				$this->_del();
				break;
			case "verify":
				$this->_verify();
				break;
			case "createylx":
				$this->_createylx();
				break;
			case "create":
				$this->_create();
				break;
			case "get_module":
				$this->_get_module();
				break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
		//搜索条件
		$condition = 'where project.project_step=1 and project.status=1 order by project.id desc';
		
		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$project_array = $this->obj_project->listProject($condition,$this->obj_page);
		$page_list = $this->obj_page->show(2);
		
		if (is_array($project_array)){
			//审批流程
			$flow_array = include('flow_config.ini.php');
			for ($i=0;$i<count($project_array);$i++){
				//对时间进行格式化
				$project_array[$i]['draft_date'] = @date("Y-m-d",$project_array[$i]['draft_date']);
				
				if ($project_array[$i]['is_submit'] == 0) {
					if ($project_array[$i]['nowflow_id']) {
						//获取审批明细
						$verify_array = $this->obj_project_verify->getProjectVerify($project_array[$i]['nowflow_id']);
						$flow_name = $flow_array["{$verify_array['verify_flow']}"]['name'];
						if ($verify_array['verify_status'] == 1) {
							$verify_status = '审批通过';
						}elseif ($verify_array['verify_status'] == 2){
							$verify_status = '<font color=red>审批打回</font>';
						}else{
							$verify_status = '待审批';
						}
					}else {
						$flow_name = '无';
						$verify_status = '未提交';
					}
				}else {
					if ($project_array[$i]['nowflow_id']) {
						//获取审批明细
						$verify_array = $this->obj_project_verify->getProjectVerify($project_array[$i]['nowflow_id']);
						$flow_name = $flow_array["{$verify_array['verify_flow']}"]['name'];
						if ($verify_array['verify_status'] == 1) {
							$verify_status = '审批通过';
						}elseif ($verify_array['verify_status'] == 2){
							$verify_status = '<font color=red>审批打回</font>';
						}else{
							$verify_status = '待审批';
						}
					}else {
						$flow_name = '无';
						$verify_status = '已提交';
					}
				}
				$project_array[$i]['flow_name'] = $flow_name;
				$project_array[$i]['verify_status'] = $verify_status;
			}
		}
		
		/**
		 * 页面输出
		 */
		$this->output("user_id", $_SESSION['s_admin']['id']);
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('project_array',$project_array);
		$this->output('page_list',$page_list);
		$this->output('group_name',$_SESSION['s_admin']['group_name']); //角色
		$this->output('condition',$this->_input);
		$this->showpage('sys_project_apply.list');
	}
	
	/**
	 * 我的列表
	 */
	function _mylist(){
		
		$this->showpage('sys_project_apply.mylist');
	}
	
	/**
	 * 信息添加
	 */
	function _add(){
		/**
		 * 页面输出
		 */
		$this->output('nowDate', date('Y-m-d'));
		$this->showpage('sys_project_apply.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["txtProjectName"], "require"=>"true", "message"=>'项目名称不能为空'),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong'])
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$value_array = array();
			$value_array['project_name'] = trim($this->_input["txtProjectName"]);  //项目名称
			$value_array['draft_date'] = strtotime($this->_input["txtDraftDate"]);  //比稿预计期限
			$value_array['draft_budget'] = trim($this->_input["txtDraftBudget"]);  //比稿预算
			$value_array['director_id'] = $this->_input["txtDirectorId"];  //选择总监
			$value_array['background'] = Common::replacebr($this->_input["txtBackground"]);  //项目背景
			$value_array['content_intro'] = Common::replacebr($this->_input["txtContentIntro"]);  //比稿内容说明
			$value_array['service'] = Common::replacebr($this->_input["txtService"]);  //未来项目服务内容
			$value_array['other_intro'] = Common::replacebr($this->_input["txtOtherIntro"]);  //其他参与比稿的公司简介
			$value_array['is_submit'] = $this->_input["submit_type"];  //是否提交申请
			$value_array['is_end'] = '0';  //审批是否结束
			$value_array['project_step'] = 1;  //项目阶段（1预立项申请2预立项3执行4关闭）
			$value_array['nowflow_id'] = 0;  //当前流程ID
			$value_array['nextflow_id'] = 0;  //下个流程ID
			$value_array['user_id'] = $_SESSION['s_admin']['id'];  //提交人
			$value_array['create_time'] = time();  //提交时间
			$value_array['status'] = 1;  //状态
			$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID(外键)
			$value_array['customer_id'] = $this->_input["hidCustomerId"];  //客户联系人ID(外键)
			$value_array['member_id'] = $this->_input["hidMemberId"];  //比稿人员ID（外键）
			$result = $this->obj_project->addProject($value_array);
			
			if ($result != true){
				$this->redirectPath('error','','创建预立项申请失败！',1);
			}else {
				//提交审批流程
				if ($this->_input["submit_type"]) {
					$flow_array = include('flow_config.ini.php');
					$roles = $flow_array['1']['roles'];
					if (is_array($roles)) {
						$project_id = mysql_insert_id();
						
						$tmp_array = array();
						$tmp_array['id'] = $project_id;
						
						$i = 0;
						foreach ($roles as $val){
							$value_array = array();
							$value_array['project_id'] = $project_id;
							$value_array['verify_flow'] = '1';
							$value_array['verify_role'] = $val;
							$value_array['assign_time'] = time();
							$value_array['member_id'] = '0';
							$value_array['verify_time'] = '0';
							$value_array['verify_options'] = '';
							$value_array['verify_status'] = '0';
							$result = $this->obj_project_verify->addProjectVerify($value_array);
							
							//下一步审批流程
							if ($i == 0) {
								$newid = mysql_insert_id();
								$tmp_array['nextflow_id'] = $newid;
							} 
							$i++;
						}
						
						$this->obj_project->updateProject($tmp_array);
					}
				}
				
				//记录操作日志
				SystemPowerClass::addSysLog('项目->预立项申请管理->创建预立项申请');
				$this->redirectPath('error','./project.manage.php?action=list','创建预立项申请成功！',1);
			}
//		}
	}
	
	/**
	 * 修改信息
	 */
	function _modi(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'项目ID不能为空'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$project_array = $this->obj_project->getProject($this->_input["id"], 'more');
			if ($project_array){
				
				//格式化比稿预计期限
				$project_array['draft_date'] = date('Y-m-d', $project_array['draft_date']);
				//获取比稿人员名称
				$member_name = '';
				if ($project_array['member_id'] != '') {
					$ids = explode(',', $project_array['member_id']);
					foreach ($ids as $v) {
						$member_array = $this->obj_system_power->getSysMemberById($v);
						$member_name .= ','.$member_array['real_name'];
					}
				}
				$project_array['member_name'] = $member_name ? substr($member_name, 1) : '';
				/**
				 * 页面输出
				 */
				$this->output("curpage", $this->_input['curpage']);
				$this->output('project_array',$project_array);
				$this->output('condition',$this->_input);
				$this->showpage('sys_project_apply.modi');
			}else {
				$this->redirectPath('error','','项目不存在或ID非法',1);
			}
		}
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsIdIsEmpty']),
//			array("input"=>$this->_input["txttitle"], "require"=>"true", "message"=>$this->_lang['errSysNNewsNameIsEmpty']),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong']),		
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['project_name'] = trim($this->_input["txtProjectName"]);  //项目名称
			$value_array['draft_date'] = strtotime($this->_input["txtDraftDate"]);  //比稿预计期限
			$value_array['draft_budget'] = trim($this->_input["txtDraftBudget"]);  //比稿预算
			$value_array['director_id'] = $this->_input["txtDirectorId"];  //选择总监
			$value_array['background'] = Common::replacebr($this->_input["txtBackground"]);  //项目背景
			$value_array['content_intro'] = Common::replacebr($this->_input["txtContentIntro"]);  //比稿内容说明
			$value_array['service'] = Common::replacebr($this->_input["txtService"]);  //未来项目服务内容
			$value_array['other_intro'] = Common::replacebr($this->_input["txtOtherIntro"]);  //其他参与比稿的公司简介
			$value_array['is_submit'] = $this->_input["submit_type"];  //是否提交申请
			$value_array['nowflow_id'] = '0';  //
			$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID(外键)
			$value_array['customer_id'] = $this->_input["hidCustomerId"];  //客户联系人ID(外键)
			$value_array['member_id'] = $this->_input["hidMemberId"];  //比稿人员ID（外键）
			$result = $this->obj_project->updateProject($value_array);
			if ($result != true){
				$this->redirectPath('error','','更新预立项申请失败！',1);
			}else {
				//提交审批流程
				if ($this->_input["submit_type"]) {
					$flow_array = include('flow_config.ini.php');
					$roles = $flow_array['1']['roles'];
					if (is_array($roles)) {
						$project_id = $this->_input["id"];
						
						$tmp_array = array();
						$tmp_array['id'] = $project_id;
						
						$i = 0;
						foreach ($roles as $val){
							$value_array = array();
							$value_array['project_id'] = $project_id;
							$value_array['verify_flow'] = '1';
							$value_array['verify_role'] = $val;
							$value_array['assign_time'] = time();
							$value_array['member_id'] = '0';
							$value_array['verify_time'] = '0';
							$value_array['verify_options'] = '';
							$value_array['verify_status'] = '0';
							$result = $this->obj_project_verify->addProjectVerify($value_array);
							//下审批流程
							if ($i == 0) {
								$newid = mysql_insert_id();
								$tmp_array['nextflow_id'] = $newid;
							} 
							$i++;
						}
						$this->obj_project->updateProject($tmp_array);
					}
				}
				
				//记录操作日志
				SystemPowerClass::addSysLog('项目->预立项申请管理->更新预立项申请');
				$this->redirectPath('error','./project.manage.php?action=list','更新预立项申请成功！',1);
			}
//		}
	}
	/**
	 * 查看
	 */
	function _view(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$project_array = $this->obj_project->getProject($this->_input["id"], 'more');
			//print_r($project_array);
			if ($project_array) {
				//获取提交人
//				$member_array = $this->obj_system_power->getSysMemberById($project_array['user_id']);
//				$project_array['user_name'] = $member_array['real_name'];
				//获取比稿人员名称
				$member_name = '';
				if ($project_array['member_id'] != '') {
					$ids = explode(',', $project_array['member_id']);
					foreach ($ids as $v) {
						$member_array = $this->obj_system_power->getSysMemberById($v);
						$member_name .= ','.$member_array['real_name'];
					}
				}
				$project_array['member_name'] = $member_name ? substr($member_name, 1) : '';
				
				//格式化日期
				$project_array['draft_date'] = date('Y-m-d', $project_array['draft_date']);
				$project_array['create_time'] = date('Y-m-d', $project_array['create_time']);
				
				//审批流程
				$flow_array = include('flow_config.ini.php');
					
				//获取审批明细
				$verify_array = array();
				//如果当前用户是审批人，显示新的审批流程
				$is_verify = $this->_input['verify'];
				if ($is_verify == 'do') { //审批
					$vid = $project_array['nextflow_id'];
				}else { //查看
					$vid = $project_array['nowflow_id'];
				}
				if ($vid != 0) {
					//根据下一步流程ID获取信息
					$next_array = $this->obj_project_verify->getProjectVerify($vid);
					//获取当前审批阶段
					$project_array['flow_name'] = $flow_array[$next_array['verify_flow']]['name'];
					
					$where = " where project_id='".$project_array['id']."' and verify_flow='".$next_array['verify_flow']."' order by id asc";
					$verify_array = $this->obj_project_verify->listProjectVerify($where, $obj_page);
					if ($verify_array) {
						$i = 0;
						foreach ($verify_array as $key => $val){
							$verify_array[$key]['verify_level'] = $this->level[$i];
							//获取角色名
							$group_array = $this->obj_system_power->getSysGroupById($val['verify_role']);
							if ($group_array) {
								$verify_array[$key]['verify_role'] = $group_array['sys_group_name'];
							}
							if ($val['member_id']) {
								$member_array = $this->obj_system_power->getSysMemberById($project_array['user_id']);
								$verify_array[$key]['user_name'] = $member_array['real_name'];
							}
							if ($val['verify_time']) {
								$verify_array[$key]['verify_time'] = date('Y-m-d', $val['verify_time']);
							}
							if ($val['verify_status'] == 1) {
								$verify_array[$key]['verify_status'] = '审批通过';
							}elseif ($val['verify_status'] == 2){
								$verify_array[$key]['verify_status'] = '<font color=red>审批打回</font>';
							}else {
								$verify_array[$key]['verify_status'] = '待审批';
							}
							$i++;
						}
					}
				}
			}
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('project_array',$project_array);
			$this->output('verify_array',$verify_array);
			$this->output('verify',$this->_input['verify']);
			$this->output('vid',$vid);
			$this->showpage('sys_project_apply.view');
		}
	}
	/**
	 * 删除信息
	 */
	function _del(){
		if (is_array($this->_input['id'])){
			foreach ($this->_input['id'] as $v) {
				$this->obj_project->delProject($v);
			}
			//记录操作日志
			SystemPowerClass::addSysLog('项目->预立项申请管理->删除预立项申请');
			$this->redirectPath('error', '' , '删除预立项申请成功！', 1);
		}else {
			$this->redirectPath('error', '', '删除预立项申请失败！', 1);
		}
	}
	function _verify(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['member_id'] = $_SESSION['s_admin']['id'];
			$value_array['verify_time'] = time();
			$value_array['verify_options'] = $this->_input["verify_options"];
			$value_array['verify_status'] = $this->_input["verify_status"];
			$result = $this->obj_project_verify->updateProjectVerify($value_array);
			if ($result != true){
				$this->redirectPath('error','','审批失败！',1);
			}else {
				//更新项目表
				$value_array = array();
				$value_array['id'] = $this->_input["project_id"];
				$value_array['nowflow_id'] = $this->_input["id"];
				$value_array['nextflow_id'] = 0;
				if ($this->_input["verify_status"] == 2) {//打回
					$value_array['is_submit'] = 0;
				}
				$this->obj_project->updateProject($value_array);
				//记录操作日志
				SystemPowerClass::addSysLog('项目->项目审批->项目审批');
				$this->redirectPath('error','./project.manage.php?action=list','审批成功！',1);
			}
		}
	}
	
	/**
	 * 创建预立项
	 */
	function _createylx(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$project_array = $this->obj_project->getProject($this->_input["id"], 'more');
			//print_r($project_array);
			if ($project_array) {
				//获取比稿人员名称
				$member_name = '';
				if ($project_array['member_id'] != '') {
					$ids = explode(',', $project_array['member_id']);
					foreach ($ids as $v) {
						$member_array = $this->obj_system_power->getSysMemberById($v);
						$member_name .= ','.$member_array['real_name'];
					}
				}
				$project_array['member_name'] = $member_name ? substr($member_name, 1) : '';
				
				//格式化日期
				$project_array['draft_date'] = date('Y-m-d', $project_array['draft_date']);
				$project_array['create_time'] = date('Y-m-d', $project_array['create_time']);
				
				
			}
			//获取财务人员
			$caiwu_array = $this->obj_member->getSystemMemberList('', $obj_page);
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('project_array',$project_array);
			$this->output('caiwu_array',$caiwu_array);
			$this->showpage('sys_project_apply.createylx');
		}
	}
	
	function _create(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["project_id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			
			$value_array = array();
			$value_array['id'] = $this->_input["project_id"];
			$value_array['project_tmpnum'] = $this->_input["tmpnum"];
			$value_array['caiwu_id'] = $this->_input["caiwu_id"];
			$value_array['project_step'] = '2';
			$value_array['is_submit'] = '0';
			$value_array['nowflow_id'] = '0';
			$result = $this->obj_project->updateProject($value_array);
			if ($result != true){
				$this->redirectPath('error','','创建失败！',1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog('项目管理->创建预立项');
				$this->redirectPath('error','./project.manage.php?action=list','创建预立项成功！',1);
			}
		}
	}
	
	/**
	 * 取模块内容
	 */
	function _get_module(){

		$module_name = $this->_input['module_name'];

		switch ($module_name){
			case "selCompany":
				$this->showpage("sys_module_company.info");
				break;
			case 'selCustomer':
				$cid = $this->_input['cid'];
				$this->output("cid", $cid);
				$this->showpage("sys_module_customer.info");
				break;
			case 'selMember':
				$dept_array = $this->obj_department->getDepartmentList(array('status' => 1), $obj_page);
				
				$this->output('dept_array',$dept_array);
				$this->showpage("sys_module_member.info");
				break;
			default:
				exit;
		}
	}

}
$project = new SysProjectClass();
$project->main();
unset($project);
?>